﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ControlPoint.ControlAPI {
    public class ScriptEngine : EezeeScript.ScriptHandler {

        private EezeeScript.ScriptContext _scriptContext;
        private Main _m;

        public ScriptEngine(EezeeScript.Script script, Main m) {

            _scriptContext = new EezeeScript.ScriptContext(script);
            _scriptContext.InterruptOnCustomCommand = false;
            _scriptContext.Handler = this;

            _m = m;
        }

        public void Update() {
               _scriptContext.Execute();

        }

        public void OnScriptCommand(EezeeScript.ScriptContext scriptContext,
            String strCommand, List<object> listParameters) {

            if (strCommand == "SetValue") {
                Console.WriteLine("RUNNING SCRIPT");
                String strVariable = ((String)listParameters[0]);
                int level = (int)listParameters[1];
                ((ZWaveDimmer)this._m.Devices[strVariable]).Dim(level);
                
            } else if (strCommand == "GetValue") {
                String strVariable = ((String)listParameters[0]);
                //_scriptContext.GlobalVariables["Var"] = !(bool)_scriptContext.GlobalVariables["G_ALONE"];

                Console.ReadLine();
            } else if (strCommand == "Wait") {
                int wait = (int)listParameters[0];
                System.Threading.Thread.Sleep(wait);
                //m_iMoveX = (int)listParameters[0];
                //m_iMoveY = (int)listParameters[1];
            }
        }

        public void OnScriptVariableUpdate(EezeeScript.ScriptContext scriptContext,
            String strIdentifier, object objectValue) {
        }

        public void OnScriptInterrupt(EezeeScript.ScriptContext scriptContext) {
        }



        public EezeeScript.ScriptContext ScriptContext {
            get { return _scriptContext; }
        }

    }
}
